#include "stdafx.h"
#include "Effect/RoseBed.h"
#include "Effect/CloudEffect.h"
#include "Effect/Petals.h"
#include "SplashScreen.h"

SmartPtr <Interface_SplashScreen> g_pInterface_SplashScreen;

Interface_SplashScreen :: Interface_SplashScreen()
: Screen(), m_fStellarUpdateTime(0.0f)
{
}

Interface_SplashScreen :: ~Interface_SplashScreen()
{
	MoonImg.release();
	GlowImg.release();
	StarImg.release();
}

void Interface_SplashScreen :: Activate()
{
	mbActive = true;
}

void Interface_SplashScreen :: Deactivate()
{
	mbActive = false;
}

bool Interface_SplashScreen :: Initailize()
{
	MoonImg = SmartPtr<hgeSprite>(g_pSystem_Main->RESOURCE()->GetSprite("SplashScreen_MoonImg"));
	GlowImg = SmartPtr<hgeSprite>(g_pSystem_Main->RESOURCE()->GetSprite("SplashScreen_GlowImg"));
	StarImg = SmartPtr<hgeSprite>(g_pSystem_Main->RESOURCE()->GetSprite("SplashScreen_StarImg"));
	SkyImg	= SmartPtr<hgeSprite>(new hgeSprite(0, 0, 0, APP_WIDTH, APP_HEIGHT * 0.65f));
	SkyImg->SetHotSpot(APP_WIDTH >> 1, APP_HEIGHT >> 1);
	SkyImg->SetColor(ARGB(255, 50, 20, 20), 0);
	SkyImg->SetColor(ARGB(255, 50, 20, 20), 1);
	SkyImg->SetColor(ARGB(255, 150, 70, 70), 2);
	SkyImg->SetColor(ARGB(255, 150, 70, 70), 3);

	GlowImg->SetColor(ARGB(255, 255, 0, 0));
	MoonImg->SetColor(ARGB(255, 255, 200, 200));
	MoonImg->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);
	StarImg->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);
	mGraphic_StarData.assign(100, StarData());
	for(size_t i=0; i < mGraphic_StarData.size(); i++) // star positions
	{
		mGraphic_StarData[i].x		= g_pSystem_Main->HGESYSTEM()->Random_Float(0, APP_WIDTH) - (APP_WIDTH >> 1);
		mGraphic_StarData[i].y		= g_pSystem_Main->HGESYSTEM()->Random_Float(0, -APP_HEIGHT * 0.54f);
		mGraphic_StarData[i].scale	= g_pSystem_Main->HGESYSTEM()->Random_Float(0.1f, 0.7f);
	}
	mRoseBed = SmartPtr<Interface_SplashScreen_Effect_Rose>(new Interface_SplashScreen_Effect_Rose);
	if(!mRoseBed->Initialize())
		return false;
	mClouds = SmartPtr<Interface_SplashScreen_Effect_Cloud>(new Interface_SplashScreen_Effect_Cloud);
	if(!mClouds->Initialize())
		return false;
	mPetals = SmartPtr<Interface_SplashScreen_Effect_Petals>(new Interface_SplashScreen_Effect_Petals);
	if(!mPetals->Initialize())
		return false;
	return true;
}

void Interface_SplashScreen :: Update(const float &Interval)
{
	m_fStellarUpdateTime += Interval;
	if(m_fStellarUpdateTime > 0.064f)
	{
		for(size_t i=0; i < mGraphic_StarData.size(); i++)
			mGraphic_StarData[i].alpha = g_pSystem_Main->HGESYSTEM()->Random_Float(0.6f, 1.0f);
		m_fStellarUpdateTime = 0.0f;
	}
	mRoseBed->Update(Interval);
	mClouds->Update(Interval);
	mPetals->Update(Interval);
}

void Interface_SplashScreen :: Render()
{
	SkyImg->Render(0, 0);
	
	for(size_t i = 0 ; i < mGraphic_StarData.size(); i++)
	{
		StarImg->SetColor(ARGB(mGraphic_StarData[i].alpha * 255.0f, 255, 255, 255));
		StarImg->RenderEx(mGraphic_StarData[i].x, mGraphic_StarData[i].y, 0.0f, mGraphic_StarData[i].scale);
	}

	GlowImg->SetColor(ARGB(255, 255, 0, 0));
	GlowImg->RenderEx(0, -100, 0, 2.0f, 2.0f);

	MoonImg->RenderEx(0, -98, 0, 1.3f, 1.3f);
	
	GlowImg->SetColor(ARGB(255, 255, 0, 0));
	GlowImg->RenderEx(0, -100, 0, 1.0f, 1.0f);

	mRoseBed->Render();
	mPetals->Render();
	mClouds->Render();
}